package cn.edu.huas.shopping.controller;

import cn.edu.huas.shopping.exception.UsernameExitsException;
import cn.edu.huas.shopping.exception.UsernameOrPwdErrorException;
import cn.edu.huas.shopping.pojo.ResponseResult;
import cn.edu.huas.shopping.pojo.User;
import cn.edu.huas.shopping.service.UserService;
import cn.edu.huas.shopping.service.UserServiceImpl;
import cn.edu.huas.shopping.utils.JsonUtils;
import cn.edu.huas.shopping.utils.PayLoadDataReader;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.MultipartConfig;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.io.PrintWriter;
@MultipartConfig//支持文件上传，并对开启multipart/form-data的获取
@WebServlet("*.user")
public class UserManagerAction extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置响应字符串
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html:charset=utf-8");
        PrintWriter out = response.getWriter();

        //获得业务逻辑处理
        UserService userService=new UserServiceImpl();
        String servletpath=request.getServletPath();


        if ("/login.user".equals(servletpath)) {
            //执行登陆判断
/*          //获得用户提交的请求参数<input type="text" name="username">
            String username = request.getParameter("username");
            //<input type="text" name="pwd">
            String pwd=request.getParameter("password");*/

            //通过流的方式来获取负载的数据(request)
            String data=PayLoadDataReader.parse(request);

            //将符合json格式的data字符串,转换成java对象
            User u=JsonUtils.jsonToPojo(data,User.class);
            ResponseResult<User> result=null;
            try {
                User user=userService.login(u.getUsername(),u.getPwd());

                //如果用户登录成功，需要将用户存储在session中,就算没有session也要创建
                HttpSession session=request.getSession();
                session.setAttribute("user",user);

                //20021自定义的一个业务编码,表示登录成功
                result=new ResponseResult<>(true,"ok",20021,user);
            }catch (UsernameOrPwdErrorException e){
                result=new ResponseResult<>(false,e.getMessage(),40021);
            }catch (Exception e){
                result=new ResponseResult<>(false,"服务器繁忙",40021);
            }
            System.out.println(JsonUtils.objectToJson(result));
            out.println(JsonUtils.objectToJson(result));
        }
        else if("/register.user".equals(servletpath)){
            //执行注册逻辑
            ResponseResult<String> result=null;
            //执行用户名是否存在
            String username=request.getParameter("username");
            String pwd=request.getParameter("password");
            try {
                User user=new User(username,pwd);
                userService.regist(user);
                result=new ResponseResult<>(true,"ok",2022);
            }
            catch (UsernameExitsException e){
                result=new ResponseResult<>(false,e.getMessage(),5022);
            }catch (Exception e){
                result=new ResponseResult<>(false,"服务器繁忙",5022);
            }
            out.println(JsonUtils.objectToJson(result));
        }

        else if("/check.user".equals(servletpath)){
            ResponseResult<String> result=null;
            //执行用户名是否存在
            String username=request.getParameter("username");
            //tomcat8之后，git请求参数会被tomcat使用iso-88591的方式处理
            //username=new String(username.getBytes("utf-8"));
            try {
                userService.checkUserName(username);
                result=new ResponseResult<>(true,"ok",2022);
            }catch (UsernameExitsException e){
                result=new ResponseResult<>(false,e.getMessage(),4022);
            }catch (Exception e){
                result=new ResponseResult<>(false,"用户已存在",5022);
            }
            out.println(JsonUtils.objectToJson(result));
        }
    }
}
